home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Sample Code Update 01⁄96 / Fragment Tool / Sources / Utilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-20  |  4.0 KB  |  207 lines  |  [TEXT/MPCC]

  1. /*
  2.     File:        Utitlities.c
  3.  
  4.     Contains:    General utility routines
  5.  
  6.     Written by:    Chris White, Developer Technical Support
  7.     
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9.     
  10.     Change History (most recent first):
  11.     
  12.                   9/28/95    CW        First release
  13.  
  14. */
  15.  
  16.  
  17. #ifndef __TYPES__
  18.     #include <Types.h>
  19. #endif
  20.  
  21. #ifndef __TEXTUTILS__
  22.     #include <TextUtils.h>
  23. #endif
  24.  
  25. #ifndef __DIALOGS__
  26.     #include <Dialogs.h>
  27. #endif
  28.  
  29.  
  30. #ifndef __CODEFRAGMENTS__
  31.     #include <CodeFragments.h>
  32. #endif
  33.  
  34. #include <Folders.h>
  35. #include <Resources.h>
  36.  
  37.  
  38.  
  39.  
  40.  
  41. #include "FragmentTool.h"
  42. #include "Utilities.h"
  43.  
  44. #include "ProtoTypes.h"
  45.  
  46.  
  47.  
  48. //
  49. // Tell the user that something went wrong
  50. //
  51. void AlertUser ( short messageCode, short errorNum, StringPtr theString )
  52. {
  53.     Str255        messageString;
  54.     Str255        numberString;
  55.     
  56.     if ( messageCode > 0 )
  57.         GetIndString ( messageString, rErrorStrings, messageCode );
  58.     else
  59.         messageString[0] = '\0';
  60.      
  61.     if ( errorNum != 0 )
  62.         NumToString ( errorNum, numberString );
  63.     else
  64.         numberString[0] = '\0';
  65.      
  66.     ParamText ( messageString, numberString, theString, (StringPtr) "\p" );
  67.     
  68.     StopAlert ( kErrorDialog, nil );
  69.     
  70.     // We need to clear the param text so it isn't used by mistake
  71.     ParamText ( (StringPtr) "\p", (StringPtr) "\p", (StringPtr) "\p", (StringPtr) "\p" );
  72.     
  73.     return;
  74. }
  75.  
  76.  
  77.  
  78. pascal void OutlineUserItem ( DialogRef theDialog, short theItem )
  79. {
  80.     short    theType;
  81.     Handle    theHan;
  82.     Rect    theRect;
  83.     
  84.     GetDialogItem ( theDialog, theItem, &theType, &theHan, &theRect );
  85.     PenNormal ( );
  86.     InsetRect ( &theRect, -1, -1 );
  87.     FrameRect ( &theRect );
  88.     
  89.     return;
  90. }
  91.  
  92.  
  93.  
  94. StringPtr CopyPStr ( Str255    inSourceStr, StringPtr outDestStr, int16 inDestSize )
  95. {
  96.     int8 dataLen = inSourceStr[0] + 1;
  97.     if ( dataLen > inDestSize )
  98.         dataLen = inDestSize;
  99.     
  100.     BlockMoveData ( inSourceStr, outDestStr, dataLen );
  101.     outDestStr[0] = dataLen - 1;
  102.     return outDestStr;
  103. }
  104.  
  105.  
  106.  
  107. StringPtr ConcatPStr ( Str255 ioFirstStr, Str255 inSecondStr, int16 inDestSize )
  108. {
  109.     int8 charsToCopy = inSecondStr[0];
  110.     
  111.     if ( ioFirstStr[0] + charsToCopy > inDestSize - 1 )
  112.         charsToCopy = inDestSize - 1 - ioFirstStr[0];
  113.  
  114.     BlockMoveData ( inSecondStr + 1,  ioFirstStr + ioFirstStr[0] + 1, charsToCopy );
  115.     ioFirstStr[0] += charsToCopy;
  116.     
  117.     return ioFirstStr;
  118. }
  119.  
  120.  
  121.  
  122. StringPtr OSTypeToPStr ( OSType inOSType, StringPtr outString )
  123. {
  124.     BlockMoveData ( &inOSType, outString + 1, sizeof ( OSType ) );
  125.     outString[0] = sizeof ( OSType );
  126.  
  127.     return outString;
  128. }
  129.  
  130.  
  131.  
  132. void BlockClear ( Ptr ptr, char value, Size size )
  133. {
  134.     int i;
  135.     
  136.     for ( i = 0; i < size; i++ )
  137.         *ptr++ = value;
  138.     
  139.     return;
  140. }
  141.  
  142.  
  143.  
  144. Boolean IsAResource ( Handle theHan )
  145. {
  146.     const short kResNotFound = -1;
  147.     return HomeResFile ( theHan ) != kResNotFound;
  148. }
  149.  
  150.  
  151.  
  152. OSErr CreateTemporaryFile ( FSSpecPtr theSpec )
  153. {
  154.     OSErr        theErr = noErr;
  155.     int16        theVol;
  156.     int32        theDir;
  157.     Str255        tmpStr = "\p";
  158.     Str255        theName = "\pFragmentTool ";
  159.     
  160.     
  161.     
  162.     theErr = FindFolder ( kOnSystemDisk, kTemporaryFolderType, kCreateFolder, &theVol, &theDir );
  163.     if ( theErr ) goto CleanupAndBail;
  164.     
  165.     OSTypeToPStr ( TickCount ( ), tmpStr );
  166.     ConcatPStr ( theName, tmpStr, sizeof ( Str255 ) );
  167.     theErr = FSMakeFSSpec ( theVol, theDir, theName, theSpec );
  168.     if ( theErr != fnfErr )
  169.     {
  170.         theErr = (theErr == fnfErr) ? dupFNErr : theErr;
  171.         goto CleanupAndBail;
  172.     }
  173.     
  174.     theErr = FSpCreate ( theSpec, kFourQuestionMarks, kCFragLibraryFileType, smSystemScript );
  175.     if ( theErr ) goto CleanupAndBail;
  176.     
  177.     // The file has been created with both forks, but it doesn't have a
  178.     // resource map yet. If we try to open it now, we'll get an eofErr (-39).
  179.     FSpCreateResFile ( theSpec, kFourQuestionMarks, kCFragLibraryFileType, smSystemScript );
  180.     
  181.     
  182.     // goto's are okay for error handling
  183. CleanupAndBail:
  184.     return theErr;
  185. }
  186.  
  187.  
  188.  
  189. #if DEBUGGING
  190. void DebugStrNum ( Str255 str, long num )
  191. {
  192.     Str255 debug_str, tmp_str;
  193.     
  194.     BlockMoveData ( &str[0], &debug_str[0], str[0] + 1 );
  195.     
  196.     NumToString ( num , &tmp_str[0] );
  197.     debug_str[debug_str[0] + 1] = ' ';
  198.     BlockMoveData ( &tmp_str[1], &debug_str[debug_str[0] + 2], tmp_str[0] );
  199.     debug_str[0] = debug_str[0] + tmp_str[0] + 1;
  200.     DebugStr ( debug_str );
  201.     
  202.     return;
  203. }
  204. #endif
  205.  
  206.  
  207.